package com.gouzheng.controller;

import com.gouzheng.entity.Remarks;
import com.gouzheng.entity.User;
import com.gouzheng.service.RemarksService;
import com.gouzheng.util.BaseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/remarks")
public class RemarksController {

    @Autowired
    private RemarksService remarksService;

    //查询全部留言
    @RequestMapping("/findAllRemarks")
    public String findRemarks(Remarks remarks, Model model){
        List<Remarks> remarksList = remarksService.findRemarks(remarks);
        model.addAttribute("remarks",remarksList);
        return "remarks";
    }

    //跳转到留言界面
    @RequestMapping("/toRemarks")
    public String toRemarks(){
        return "leave";
    }
    //新增留言
    @RequestMapping("/addRemarks")
    public String addRemarks(Remarks remarks){
        remarksService.addRemarks(remarks);
        return "redirect:/remarks/findAllRemarks";
    }

//    根据学籍号查询留言
    @RequestMapping("/getStuNum")
    public String getStuNum(String stuNum,Model model){
        Remarks reply = remarksService.getReply(stuNum);
        model.addAttribute("reply",reply);
        return "stuRemark";
    }

    //学生添加留言
    @RequestMapping("/stuAddRemarks")
    public String stuAddRemarks(Remarks remarks, HttpSession session){
        User user = (User) session.getAttribute("user");
        remarks.setStuName(user.getUsername());
        remarksService.addRemarkss(remarks);
        session.setAttribute("msg","留言成功");
        return "stuRemark";

    }

    //批量删除
    @RequestMapping("/delete")
    @ResponseBody
    public void delete(HttpServletRequest request,HttpServletResponse response){
        String remarks = request.getParameter("remarks");
        List<String> delList=new ArrayList<String>();
        String[] strs=remarks.split(",");
        for(String str : strs){
            delList.add(str);
        }
        remarksService.baseList(delList);
    }









}
